﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace JMT.Data
{
    public static class ClassCollectionExtention
    {
        public static int GetUniqueIndex<T>(this IJContainer<T> jContainer) where T : JModel
        {
            List<T> list = jContainer.ToList();
            int maxIndex = 0;
            foreach (var item in list)
            {
                string strIndex = item.Name.Replace(item.Type, string.Empty);
                int index = 0;
                if (int.TryParse(strIndex, out index))
                {
                    if (index > maxIndex)
                    {
                        maxIndex = index;
                    }
                }
            }
            return maxIndex + 1;
        }

        public static int GetUniqueIndex(this ObservableCollection<JPackage> collection)
        {
            int maxIndex = 0;
            foreach (var item in collection)
            {
                string strIndex = item.Name.Replace("NewPackage", string.Empty);
                int index = 0;
                if (int.TryParse(strIndex, out index))
                {
                    if (index > maxIndex)
                    {
                        maxIndex = index;
                    }
                }
            }
            return maxIndex + 1;
        }
        public static string GetUniqueName(this ObservableCollection<JPackage> collection)
        {
            return string.Format("NewPackage{0}", collection.GetUniqueIndex());
        }

        public static int GetUniqueIndex(this ObservableCollection<JClass> collection)
        {
            int maxIndex = 0;
            foreach (var item in collection)
            {
                string strIndex = item.Name.Replace("NewClass", string.Empty);
                int index = 0;
                if (int.TryParse(strIndex, out index))
                {
                    if (index > maxIndex)
                    {
                        maxIndex = index;
                    }
                }
            }
            return maxIndex + 1;
        }
        public static string GetUniqueName(this ObservableCollection<JClass> collection)
        {
            return string.Format("NewClass{0}", collection.GetUniqueIndex());
        }

        public static int GetUniqueIndex(this ObservableCollection<JDiagram> collection)
        {
            int maxIndex = 0;
            foreach (var item in collection)
            {
                string strIndex = item.Name.Replace("NewDiagram", string.Empty);
                int index = 0;
                if (int.TryParse(strIndex, out index))
                {
                    if (index > maxIndex)
                    {
                        maxIndex = index;
                    }
                }
            }
            return maxIndex + 1;
        }
        public static string GetUniqueName(this ObservableCollection<JDiagram> collection)
        {
            return string.Format("NewDiagram{0}", collection.GetUniqueIndex());
        }

        public static int GetUniqueIndex(this ObservableCollection<JProperty> collection)
        {
            int maxIndex = 0;
            foreach (var item in collection)
            {
                string strIndex = item.Name.Replace("NewProperty", string.Empty);
                int index = 0;
                if (int.TryParse(strIndex, out index))
                {
                    if (index > maxIndex)
                    {
                        maxIndex = index;
                    }
                }
            }
            return maxIndex + 1;
        }
        public static string GetUniqueName(this ObservableCollection<JProperty> collection)
        {
            return string.Format("NewProperty{0}", collection.GetUniqueIndex());
        }

        public static int GetUniqueIndex(this ObservableCollection<JMethod> collection)
        {
            int maxIndex = 0;
            foreach (var item in collection)
            {
                string strIndex = item.Name.Replace("NewMethod", string.Empty);
                int index = 0;
                if (int.TryParse(strIndex, out index))
                {
                    if (index > maxIndex)
                    {
                        maxIndex = index;
                    }
                }
            }
            return maxIndex + 1;
        }
        public static string GetUniqueName(this ObservableCollection<JMethod> collection)
        {
            return string.Format("NewMethod{0}", collection.GetUniqueIndex());
        }

        public static int GetUniqueIndex(this ObservableCollection<JInterface> collection)
        {
            int maxIndex = 0;
            foreach (var item in collection)
            {
                string strIndex = item.Name.Replace("NewInterface", string.Empty);
                int index = 0;
                if (int.TryParse(strIndex, out index))
                {
                    if (index > maxIndex)
                    {
                        maxIndex = index;
                    }
                }
            }
            return maxIndex + 1;
        }
        public static string GetUniqueName(this ObservableCollection<JInterface> collection)
        {
            return string.Format("NewInterface{0}", collection.GetUniqueIndex());
        }

        public static int GetUniqueIndex(this ObservableCollection<JRelation> collection)
        {
            int maxIndex = 0;
            foreach (var item in collection)
            {
                string strIndex = item.Name.Replace("NewRelation", string.Empty);
                int index = 0;
                if (int.TryParse(strIndex, out index))
                {
                    if (index > maxIndex)
                    {
                        maxIndex = index;
                    }
                }
            }
            return maxIndex + 1;
        }
        public static string GetUniqueName(this ObservableCollection<JRelation> collection)
        {
            return string.Format("NewRelation{0}", collection.GetUniqueIndex());
        }
    }
}
